package com.base.common.db.dao;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;

import com.base.common.bean.MemberView;
import com.base.common.db.DBConstants;
import com.base.common.db.constant.ConstantUserInfoTable;
import com.base.common.util.ObjectUtil;
import com.base.common.util.StringUtils;
import com.base.common.util.log.LogUtil;

import java.io.File;

public class UserInfoDAO {
	private static final String TAG = UserInfoDAO.class.getName();
	private Context context;
	/** 表URI **/
	private static Uri urString;
	
	static {
		urString = Uri.parse(DBConstants.ContentProvider.PROTOCOL
				+ DBConstants.ContentProvider.AUTHORITY + File.separator
				+ ConstantUserInfoTable.TABLENAME);
	}

	public UserInfoDAO(Context context) {
		this.context = context;
	}

	/** 根据userid获取单个用户 **/
	public MemberView getUserInfoByUserId(String userid) {
		String sql = "SELECT * FROM " + ConstantUserInfoTable.TABLENAME + " where "
				+ ConstantUserInfoTable.USERID + " ='"+userid+"'";
		Cursor cursor = context.getContentResolver().query(urString, null,
				"sql://" + sql, null, null);
		MemberView userInfo = null;
		if (cursor != null && cursor.moveToFirst()) {
			Object obj = null;
			try {
				obj = ObjectUtil.convertCursorToObject(cursor, MemberView.class);
			} catch (IllegalArgumentException e) {
				LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
			} catch (IllegalAccessException e) {
				LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
			}
			if (obj instanceof MemberView)
				userInfo = (MemberView) obj;
		}
		if (cursor != null)
			cursor.close();
		return userInfo;
	}
	
	/** 获取用户信息 **/
	public MemberView getUserInfo() {
		String sql = "SELECT  * FROM " + ConstantUserInfoTable.TABLENAME ;
		Cursor cursor = context.getContentResolver().query(urString, null,
				"sql://" + sql, null, null);
		MemberView userInfo = null;
		if (cursor != null && cursor.moveToFirst()) {
			Object obj = null;
			try {
				obj = ObjectUtil.convertCursorToObject(cursor, MemberView.class);
			} catch (IllegalArgumentException e) {
				LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
			} catch (IllegalAccessException e) {
				LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
			}
			if (obj instanceof MemberView)
				userInfo = (MemberView) obj;
		}
		if (cursor != null)
			cursor.close();
		return userInfo;
	}
	

	/** 保存数据 **/
	public long save(MemberView userinfo) {
		long result = 0;
		try {
			ContentResolver contentResolver = context.getContentResolver();
			ContentValues values = new ContentValues();
			values.put(ConstantUserInfoTable.PARTYID, userinfo.getPartyId());
			values.put(ConstantUserInfoTable.ACCOUNT, userinfo.getLogin_account());
			values.put(ConstantUserInfoTable.EMAIL, userinfo.getEmail());
			values.put(ConstantUserInfoTable.MOBILE, userinfo.getPhone());
			values.put(ConstantUserInfoTable.PARTYNAME, userinfo.getNickname());
			values.put(ConstantUserInfoTable.PARTYDESC, userinfo.getPartyDesc());
			values.put(ConstantUserInfoTable.USERID, userinfo.getIds());
			values.put(ConstantUserInfoTable.CARTID, userinfo.getCartId());
			values.put(ConstantUserInfoTable.PROFILE, userinfo.getProfile());
			values.put(ConstantUserInfoTable.LOGINTIME, userinfo.getLoginTime());
			values.put(ConstantUserInfoTable.UNIONID, userinfo.getUnionid());
			values.put(ConstantUserInfoTable.LOGINTYPE, userinfo.getLoginType());
			Uri uri = contentResolver.insert(urString, values);
			result = ContentUris.parseId(uri);// /取得主健ID
		} catch (IllegalArgumentException e) {
			LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
		}
		return result;
	}

	/** 更新单个数据 **/
	public void update(MemberView userinfo) {
		ContentResolver contentResolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		contentResolver.update(urString, values, ConstantUserInfoTable.USERID + "='"
				+ userinfo.getUserId()+  "'", null);
	}


	/** 监听表的数据变化 **/
	public void observer(ContentObserver contentObserver) {
		context.getContentResolver().registerContentObserver(urString, true,
				contentObserver);
	}

	/**
         * 取消监听
         * @param contentObserver
         */
        public void unObserver(ContentObserver contentObserver) {
            if (contentObserver != null) {
                context.getContentResolver().unregisterContentObserver(contentObserver);
            }
        }

	/** 清空表 **/
	public int deleteAll() {
		int result = 0;
		ContentResolver contentResolver = context.getContentResolver();
		result = contentResolver.delete(urString, null, null);
		return result;
	}
}
